using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;
using DirectShowLib;
using DirectShowLib.Dvd;
using LeisureCenter.Api;

namespace DirectShowPlayer
{
    public class DirectShowVideoDvdMedia : DirectShowVideoMedia, IVideoDvdMedia, IControllableMedia
    {
        private IDvdGraphBuilder dvdGraph;
        private IDvdInfo2 dvdInfo;
        private IDvdControl2 dvdCtrl;

        public DirectShowVideoDvdMedia(DirectShowPlayer player, string fileName)
            : base(player, fileName)
        {
            this.control.MouseMove += new MouseEventHandler(fullScreenForm_MouseMove);
            this.control.MouseDown += new MouseEventHandler(fullScreenForm_MouseDown);
        }

        protected override void CreateFilterGraph()
        {
            dvdGraph = (IDvdGraphBuilder)new DvdGraphBuilder();

            int hr;
            AMDvdRenderStatus status;
            object comobj = null;

            hr = dvdGraph.RenderDvdVideoVolume(FileName, AMDvdGraphFlags.None, out status);
            DsError.ThrowExceptionForHR(hr);

            hr = dvdGraph.GetDvdInterface(typeof(IDvdInfo2).GUID, out comobj);
            DsError.ThrowExceptionForHR(hr);
            dvdInfo = (IDvdInfo2)comobj;
            comobj = null;

            hr = dvdGraph.GetDvdInterface(typeof(IDvdControl2).GUID, out comobj);
            DsError.ThrowExceptionForHR(hr);
            dvdCtrl = (IDvdControl2)comobj;
            comobj = null;

            hr = dvdGraph.GetFiltergraph(out graphBuilder);
            DsError.ThrowExceptionForHR(hr);
        }

        protected override void CloseInterfaces()
        {
            base.CloseInterfaces();
        }

        protected override void CreateFullScreenForm()
        {
            base.CreateFullScreenForm();
            this.fullScreenForm.MouseMove += new MouseEventHandler(fullScreenForm_MouseMove);
            this.fullScreenForm.MouseDown += new MouseEventHandler(fullScreenForm_MouseDown);
        }

        void fullScreenForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.dvdCtrl != null)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                this.dvdCtrl.ActivateAtPosition(pt);
            }
        }

        void fullScreenForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.dvdCtrl != null)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                this.dvdCtrl.SelectAtPosition(pt);
            }
        }

        #region IControllableMedia Membres

        public bool HandleKey(Keys key)
        {
            switch (key)
            {
                case Keys.Up:
                    this.dvdCtrl.SelectRelativeButton(DvdRelativeButton.Upper);
                    break;
                case Keys.Down:
                    this.dvdCtrl.SelectRelativeButton(DvdRelativeButton.Lower);
                    break;
                case Keys.Left:
                    this.dvdCtrl.SelectRelativeButton(DvdRelativeButton.Left);
                    break;
                case Keys.Right:
                    this.dvdCtrl.SelectRelativeButton(DvdRelativeButton.Right);
                    break;
                case Keys.Return:
                    this.dvdCtrl.ActivateButton();
                    break;
                default:
                    return false;
            }
            return true;
        }

        #endregion

        #region IPlaylistMedia Membres

        public void Previous()
        {
            if (this.dvdCtrl != null)
            {
                IDvdCmd cmdOption = null;
                int hr = this.dvdCtrl.PlayPrevChapter(DvdCmdFlags.Flush, out cmdOption);
                DsError.ThrowExceptionForHR(hr);

                //if( cmdOption != null )
                //{
                //    pendingCmd = true;
                //}
            }
        }

        public void Next()
        {
            if (this.dvdCtrl != null)
            {
                IDvdCmd cmdOption = null;
                int hr = this.dvdCtrl.PlayNextChapter(DvdCmdFlags.Flush, out cmdOption);
                DsError.ThrowExceptionForHR(hr);

                //if( cmdOption != null )
                //{
                //    pendingCmd = true;
                //}
            }
        }

        public IEnumerable<string> Titles
        {
            get { return new List<string>(); }
        }

        public int Count
        {
            get
            {
                return 0;
            }
        }

        public string GetTitle(int index)
        {
            return "Chapter " + index.ToString(CultureInfo.InvariantCulture);
        }

        public event EventHandler ItemIndexChanged;

        public event EventHandler PlaylistChanged;

        #endregion
    }
}
